'use strict';

const fs = require('fs');
const path = require('path');
const gulp = require('gulp');
const gutil = require('gulp-util');
const data = require('gulp-data');
const ejs = require('gulp-ejs');


const bugatti = '..';     //bugatti配置项目路径
const bugatti_file = '/config.json';
const template_path = './template';

const distDir = './dist';      // html文档生成目录

// 模版合并
gulp.task('ejs', function () {
  gulp
    .src(template_path + '/**/*.html')
    .pipe(data(function (file) {
      let filePath = file.path;
      // config.json 全局数据，页面中直接通过属性名调用
      let configPath = template_path + '/config.json';
      let option = fs.readFileSync(configPath);
      try {
        console.info(":=====>", bugatti + bugatti_file);
        option = fs.readFileSync(bugatti + bugatti_file)
      } catch (e) {
        console.warn("    未获取到bugatti配置项");
      }

      return JSON.parse(option)
    }))
    .pipe(ejs().on('error', function (err) {
      gutil.log(err);
      this.emit('end');
    }))
    .pipe(gulp.dest(distDir));
});